package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.DbusinessDao;
import cn.gson.fengboot.model.dao.DbusproDao;
import cn.gson.fengboot.model.mapper.DbusinessMapper;
import cn.gson.fengboot.model.mapper.DphaseMapper;
import cn.gson.fengboot.model.pojos.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Transactional(rollbackFor = Exception.class)
public class DbusinessService {

    @Autowired
    DbusinessMapper dbusinessMapper;
    @Autowired
    DphaseMapper dphaseMapper;
    @Autowired
    DbusinessDao dbusinessDao;
    @Autowired
    DbusproDao dbusproDao;
    @Autowired
    DloginService dloginService;



    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");

    /*我可查看的商机*/
    public List<Dbusiness> selectallbusiness(String busName, String phaseName, HttpServletRequest request){
        List<String> getbuspeoplelist = dloginService.getbuspeoplelist(request);
        List<Dbusiness> buslist = null;
        buslist = new ArrayList<>();
        if(getbuspeoplelist.size()>0){/*根据员工名称查询*/
            for (int i=0;i<getbuspeoplelist.size();i++){
                List<Dbusiness> selectbusiness = dbusinessMapper.selectbusiness(busName, getbuspeoplelist.get(i), phaseName);
                buslist.addAll(selectbusiness);//添加每一个成员的数据
            }
        }else {/*查询所有*/
             List<Dbusiness> selectbusiness = dbusinessMapper.selectbusiness(busName, "", phaseName);
             buslist.addAll(selectbusiness);//添加每一个成员的数据
        }

        return buslist;
    }


    /*我的商机*/
    public List<Dbusiness> selectmybusiness(String busName, String phaseName, HttpServletRequest request){
        QempFrom user = (QempFrom) request.getSession().getAttribute("user");
        List<Dbusiness> buslist = new ArrayList<>();
        List<Dbusiness> selectbusiness = dbusinessMapper.selectbusiness(busName, user.getEmpName(), phaseName);
        buslist.addAll(selectbusiness);//添加每一个成员的数据
        return buslist;
    }

    public List<Dbusiness> kehushangji(String search,String search2){
        return  dbusinessMapper.kehushangji(search,search2);
    }

    public List<Dphase> selectphase(){
        return dphaseMapper.selectphase();
    }


    public boolean addbusiness(Dbusiness dbusiness,List budIdlist) {

        JSONArray json = JSONArray.parseArray(String.valueOf(budIdlist)) ;
        boolean flag=true;
        try{
            Dbusiness byBusName = dbusinessDao.findByBusName(dbusiness.getBusName());

                if(dbusiness.getBusId()==0){//如果商机id为0，set建立时间
                    if(byBusName==null){//如果商机名称不存在则新增
                        dbusiness.setBusSettime(new Timestamp(new Date().getTime()));
                        flag=true;
                    }else{
                        flag=false;

                    }

                }
                dbusiness.setBusUptime(new Timestamp(new Date().getTime()));

                dbusiness.setBusStatu("有效");
                dbusiness.setDbuspros(null);
            Dbusiness save = dbusinessDao.save(dbusiness);

            dbusproDao.deleteAllBybusId(dbusiness.getBusId());

            saveProdurct(save,budIdlist);
        }catch (Exception e){
            System.out.println(e);
        }
        return flag;


    }

    /*添加购物产品*/
    public void saveProdurct(Dbusiness dbusiness,List<Dbuspro> budIdlist){


        JSONArray json = JSONArray.parseArray(String.valueOf(budIdlist)) ;
        JSONObject jsonObject = null;

        for(int i=0;i<json.size();i++){
            Integer busproId=null;
            Qproductdetail qproductdetail= new Qproductdetail();
            jsonObject = (JSONObject)json.get(i);//获取每一行转成JSON对象
            Integer prodetailId = jsonObject.getInteger("prodetailId");
            qproductdetail.setProdetailId(prodetailId);

            Integer busproNum = jsonObject.getInteger("busproNum");
            BigDecimal busproTotal = jsonObject.getBigDecimal("busproTotal");
            String busproDeiscount = jsonObject.getString("busproDeiscount");
            Dbuspro dbuspro = new Dbuspro(busproId,dbusiness,qproductdetail,busproNum,busproTotal,busproDeiscount);
            dbusproDao.save(dbuspro);
        }
    }

    public List<TfylinkmanForm> selectmainlinkman(String search, String linkmanName) {
        return dbusinessMapper.selectmainlinkman(search,linkmanName);
    }

    public List<Qproductdetail> allpro(String proName){

        return dbusinessMapper.allpro(proName);
    }

    public Dbusiness selectBybusId(Integer busId) {
        return dbusinessMapper.selectBybusId(busId);
    }

    public void detproById(Integer budproId) {
        dbusproDao.deleteById(budproId);
    }

    public Boolean detbus(String budId) {
        JSONObject obj = (JSONObject)JSONObject.parse(budId);
        List budIdlist = obj.getObject("budIdlist", List.class);
        System.out.println(budIdlist.toString());
        //转化成TSONArray
        JSONObject object=null;
        Boolean flag=false;
        JSONArray json = JSONArray.parseArray(String.valueOf(budIdlist)) ;
        for (int i=0; i<json.size();i++) {
            object = (JSONObject)json.get(i);//得到对象
            Integer businesId =object.getInteger("busId");
            flag=false;

            dbusinessMapper.selectConById(businesId);
            if(dbusinessMapper.selectConById(businesId)!=null){
                flag= true;
            }else{
                Dbusiness business = dbusinessDao.findById(businesId).get();
                business.setBusStatu("无效");
                dbusinessDao.save(business);
            }

        }

        return flag;
    }

    public void editlinkman(Integer busId, Integer linkmanId) {
        Dbusiness dbusiness = dbusinessDao.findById(busId).get();
        TfylinkmanForm t = new TfylinkmanForm();
        t.setLinkmanId(linkmanId);
        dbusiness.setBusPrimarycontact(t);
        dbusinessDao.save(dbusiness);
    }

    public List<TfyclientForm> allclientbyemp(String search, Integer empname) {
        return dbusinessMapper.allclientbyemp(search,empname);
    }
}
